package com.mlethe.calendarview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

import androidx.annotation.NonNull;

import com.mlethe.library.calendarview.Calendar;
import com.mlethe.library.calendarview.WeekMonthView;

/**
 * @Author Mlethe
 * @Date 2023/2/1
 */
public class MonthWeekView extends WeekMonthView {

    private final Paint mTextPaint = new Paint();
    private final Paint mSchemeBasicPaint = new Paint();
    private final float mRadio;
    private final float mRadius;
    private final float mSchemeBaseLine;

    public MonthWeekView(Context context) {
        super(context);
        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffFFFFFF);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(0xffed5353);
        mSchemeBasicPaint.setFakeBoldText(true);
        mRadio = dipToPx(getContext(), 7);
        mRadius = dipToPx(getContext(), 28);
        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);
    }

    /**
     * @param canvas        canvas
     * @param calendar      日历日历calendar
     * @param x             日历Card x起点坐标
     * @param y             日历Card y起点坐标
     * @param hasScheme     hasScheme 非标记的日期
     * @param weekPosition  选中周的位置【0-6】，其它为-1
     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
     */
    @Override
    protected boolean onDrawSelected(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y, boolean hasScheme, int weekPosition) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        if (weekPosition == 0) {
            Path path = new Path();
            path.moveTo(x + mItemWidth, y);
            path.lineTo(x + mRadius, y);
            path.arcTo(new RectF(
                            x,
                            y,
                            x + mRadius * 2,
                            y + mRadius * 2),
                    180,
                    90);
            path.lineTo(x, y + mRadius);
            path.lineTo(x, y + mItemHeight - mRadius);
            path.arcTo(new RectF(
                            x,
                            y + mItemHeight - mRadius * 2,
                            x + mRadius * 2,
                            y + mItemHeight),
                    180,
                    -90);
            path.lineTo(x + mRadius, y + mItemHeight);
            path.lineTo(x + mItemWidth, y + mItemHeight);
            path.close();
            canvas.drawPath(path, mSelectedPaint);
        } else if (weekPosition == 6) {
            Path path = new Path();
            path.moveTo(x, y);
            path.lineTo(x + mItemWidth - mRadius, y);
            path.arcTo(new RectF(
                            x + mItemWidth - mRadius * 2,
                            y,
                            x + mItemWidth,
                            y + mRadius * 2),
                    0,
                    -90);
            path.lineTo(x + mItemWidth, y + mRadius);
            path.lineTo(x + mItemWidth, y + mItemHeight - mRadius);
            path.arcTo(new RectF(
                            x + mItemWidth - mRadius * 2,
                            y + mItemHeight - mRadius * 2,
                            x + mItemWidth,
                            y + mItemHeight),
                    0,
                    90);
            path.lineTo(x + mItemWidth - mRadius, y + mItemHeight);
            path.lineTo(x, y + mItemHeight);
            path.close();
            canvas.drawPath(path, mSelectedPaint);
        } else {
            canvas.drawRect(x, y, x + mItemWidth, y + mItemHeight, mSelectedPaint);
        }
        return true;
    }

    @Override
    protected void onDrawScheme(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y) {
        mSchemeBasicPaint.setColor(calendar.getSchemeColor());

        canvas.drawCircle(x + mItemWidth - mRadio / 2, y + mRadio, mRadio, mSchemeBasicPaint);

        canvas.drawText(calendar.getScheme(),
                x + mItemWidth - mRadio / 2 - getTextWidth(calendar.getScheme()) / 2,
                y + mSchemeBaseLine, mTextPaint);
    }

    /**
     * 获取字体的宽
     *
     * @param text text
     * @return return
     */
    private float getTextWidth(String text) {
        return mTextPaint.measureText(text);
    }

    @Override
    protected void onDrawText(@NonNull Canvas canvas, @NonNull Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected, int weekPosition) {
        int cx = x + mItemWidth / 2;

        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + y,
                    mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + y,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
        } else if (calendar.isCurrentMonth()) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + y,
                    calendar.isCurrentWeek() ? mCurDayTextPaint : mCurMonthTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + y, mOtherMonthTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
